package com.example.miuicolorpick

import android.graphics.*
import android.util.Log
import kotlin.math.roundToInt

object DrawHelper {


    val outLineColor = Color.parseColor("#ffffffff")
    val outLineStokeWidth = 0f

    val miColors =  ColorData.getAllColor()
    var totalWidth = 0

    var curIndex = 0

    val cornerPathEffect = CornerPathEffect(20f)

    val shadowOffset = 6f

    fun drawBottom(canvas: Canvas, paint: Paint) {

        var index = 0
        val singColorWidth = getSingleWidth()

        canvas.save()
        canvas.translate(0f, getTopOffset())

        //画笔基本配置
        paint.isAntiAlias = true
        paint.style = Paint.Style.FILL
        paint.pathEffect = null

        miColors.forEach {
            paint.color = Color.parseColor(it.mainColor)
            when (index) {
                0 -> {
                    drawSingleBottomSide(
                        canvas,
                        paint,
                        true,
                        index = index,
                        singleWidth = singColorWidth
                    )
                }

                miColors.lastIndex -> {
                    drawSingleBottomSide(
                        canvas,
                        paint,
                        false,
                        index = index,
                        singleWidth = singColorWidth
                    )
                }

                else -> {
                    drawSingleBottomRect(canvas, paint, index, singColorWidth)
                }
            }

            setTargetRect(it, singColorWidth, index, getTopOffset())
            index++
        }
        canvas.restore()
    }

    fun getTopOffset():Float{
        // 计算距离顶部的距离
        val topOffset = getSingleWidth() * 2.75f
        return topOffset
    }

    fun getSingleWidth():Int{
        return totalWidth / miColors.size
    }

    /**
     * 获取底部和中间的间距
     */
    fun getMiddleOffset():Float{
        return getSingleWidth() / 4f
    }

    fun drawSingleBottomSide(
        canvas: Canvas,
        paint: Paint,
        left: Boolean,
        index: Int,
        singleWidth: Int
    ) {
        val radius = singleWidth / 2f
        if (left) {
            val cx = singleWidth / 2f
            val cy = singleWidth / 2f
            canvas.drawCircle(cx, cy, radius, paint)
            canvas.drawRect(cx, 0f, singleWidth.toFloat(), singleWidth.toFloat(), paint)
        } else {
            val cx = singleWidth * index + singleWidth / 2f
            val cy = singleWidth / 2f
            canvas.drawCircle(cx, cy, radius, paint)
            val rect =
                Rect(index * singleWidth, 0, index * singleWidth + singleWidth / 2, singleWidth)
            canvas.drawRect(rect, paint)
        }
    }

    fun drawSingleBottomRect(canvas: Canvas, paint: Paint, index: Int, singleWidth: Int) {
        val left = singleWidth * index
        val top = 0
        val botttom = singleWidth
        val right = singleWidth * (index + 1)
        val rect = Rect(left, top, right, botttom)
        canvas.drawRect(rect, paint)
    }

    fun setTargetRect(miColor: MiColor, singleWidth: Int, index: Int, topOffset: Float) {
        val left = singleWidth * index
        val top = 0
        val right = left + singleWidth
        val bottom = singleWidth
        val rect = Rect(left, top+topOffset.roundToInt(), right, bottom+topOffset.roundToInt())
        miColor.rectBottom = rect

    }


    /**
     * 绘制顶部显示栏细节
     */
    fun drawTopBar(canvas: Canvas,paint: Paint){


        val singColorWidth = getSingleWidth()
        paint.isAntiAlias = true
        paint.color = outLineColor
        paint.strokeWidth = outLineStokeWidth
        paint.style = Paint.Style.FILL
        paint.setShadowLayer(2f, 0f, 0f, Color.GRAY)
        paint.pathEffect =  cornerPathEffect
        // 绘制大范围的框框
        val path = Path()
        path.moveTo(0f + shadowOffset,0f + shadowOffset)
        path.lineTo(totalWidth.toFloat() - shadowOffset,0f + shadowOffset)
        path.lineTo(totalWidth.toFloat() - shadowOffset,getTopOffset() - getMiddleOffset())

        val selectColorSpaceWidth =  singColorWidth / 2
        val selectColorContainerWidth = singColorWidth + selectColorSpaceWidth

        path.lineTo((curIndex+1) * singColorWidth + (selectColorSpaceWidth / 2f),getTopOffset() - getMiddleOffset())
        path.lineTo((curIndex+1) * singColorWidth + (selectColorSpaceWidth / 2f),getTopOffset() + singColorWidth + selectColorSpaceWidth /2)
        path.lineTo(curIndex * singColorWidth - (selectColorSpaceWidth / 2f),getTopOffset() + singColorWidth + selectColorSpaceWidth /2)
        path.lineTo(curIndex * singColorWidth - (selectColorSpaceWidth / 2f),getTopOffset() - getMiddleOffset())
        path.lineTo(0f+shadowOffset,getTopOffset() - getMiddleOffset())
        path.close()
        canvas.drawPath(path,paint)
        paint.setShadowLayer(0f,0f,10f,Color.BLACK)
//
//        val  sha = RadialGradient(300f,300f,123f, arrayOf({Color.argb(100,0x00,0x00,0x00)}),)
////        Shader.TileMode.MIRROR);
//        paint.setShader(); //将实例化且设置好的Shader传入paint

        // 绘制选中颜色
        paint.pathEffect = null
        paint.color = Color.parseColor(miColors[curIndex].mainColor)
        canvas.drawRoundRect(RectF(miColors[curIndex].rectBottom),10f,10f,paint)

        // 绘制选中的系列颜色
        miColors[curIndex].subColors.forEachIndexed { index, item ->
            paint.color = Color.parseColor(item)
            val r = getTopOffset() / 5.3333f
            val spaceOffset = (totalWidth - miColors[curIndex].subColors.size * 2 * r) /(miColors[curIndex].subColors.size + 1)
            val cx = spaceOffset * (index + 1) + (index * r * 2  + r)
            val cy = getTopOffset() / 2
            canvas.drawCircle(cx,cy,r,paint)
        }
    }

}